import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {
        test1();
        System.out.println("------------------------------");
        test2();
        System.out.println("------------------------------");
        test3();
    }
    public static void test1(){
        List<String> names = new ArrayList<>();
        names.add("zhangsan");
        names.add("lisi");
        names.add("wangwu");
        names.add("zhaoliu");
        names.add("qianqi");
        names.add("Jack");
        names.add("Tom");
        List<String> list1 = new ArrayList<>();
        names.forEach(name -> {
            if (name.startsWith("z")) {
                list1.add(name);
            }
        });
        List<String> list2 = new ArrayList<>();
        list1.forEach(name -> {
            if (name.length() == 8) {
                list2.add(name);
            }
        });
        list2.forEach(System.out::println);

        System.out.println("------------------------------");
        names.stream().filter(s -> s.startsWith("z"))
                .filter(s -> s.length() == 8)
                .forEach(System.out::println);

        System.out.println("------------------------------");
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);

//        List<Integer> list3= list.stream().filter(integer -> integer%2==0).collect(Collectors.toList());
        List<Integer> list4 = list.stream().filter(integer -> integer % 2 == 0).toList();
        System.out.println(list4);

    }
    public static void test2() {
        List<String> list = new ArrayList<>();
        list.add("zhangsan,23");
        list.add("lisi,24");
        list.add("wangwu,25");

        // 过滤年龄大于等于24岁的
        Map<String, Integer> map = list.stream()
                .filter(s -> {
                    // 将s切分开
                    String[] split = s.split(",");
                    // 拿到年龄（转成int）
                    int age = Integer.parseInt(split[1]);
                    return age >= 24;
                })
                .collect(Collectors.toMap(
                        s -> {
                            String[] split = s.split(",");
                            return split[0];
                        },
                        s -> {
                            String[] split = s.split(",");
                            return Integer.parseInt(split[1]);
                        }
                ));

        // 输出结果
        map.forEach((k, v) -> {
            System.out.println(k + "--" + v);
        });
    }

    public static void test3(){
        List<String> list =new ArrayList<>();
        list.add("佟大为");
        list.add("张学友");
        list.add("周杰伦");
        list.add("王力宏");
        List<String> list1 =new ArrayList<>();
        list1.add("古力娜扎");
        list1.add("杨幂");
        list1.add("赵丽颖");
        Stream<String> stream1=list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length()==3;
            }
        }).limit(2);
        Stream<String> stream2 = list1.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("杨");
            }
        }).skip(1);
        Stream.concat(stream1,stream2).forEach(System.out::println);
    }
}

